﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WFControlLibrary;
namespace 任务状态
{
    class Buy
    {

        public int Quantity = 0;//数量
        public double BuyPrice = -1;//当时价
        public double Funds = 10000;//池
        public DateTime BuyDate;//日期
        public DateTime SellDate;//日期
        public string StockName = "";

        // 买入股票
        public String BuyStock(string stockName, double price)
        {
          if(stockName != this.StockName)
            {
                SwitchStock(stockName);
                this.StockName = stockName;
            }
            // 计算资金池最多可以购买的股数，并确保最低100股起
            int maxQuantity = (int)(this.Funds / price); // 计算最多可以买多少股
            int buyQuantity = (maxQuantity / 100) * 100; // 取100的倍数

            // 检查是否至少能买100股
            if (buyQuantity < 100)
            {
                Console.WriteLine("资金不足，无法购买至少100股。");
                return "资金不足，无法购买至少100股。";
            }

            // 更新资金池和持仓信息
            this.Quantity = buyQuantity;
            this.BuyPrice = price;
            this.Funds -= buyQuantity * price; // 扣除买入资金
            this.BuyDate = DateTime.Now;
            Console.WriteLine($"已买入 {buyQuantity} 股 {stockName}，每股价格为 {price}，剩余资金为 {Funds}");
            SaveStock(stockName);
            return $"已买入 {buyQuantity} 股 {stockName}，每股价格为 {price}，剩余资金为 {Funds}";
        }
        public double getFunds()
        {
            return Funds;
        }
        public double getValue(double price)
        {
            return price* Quantity;
        }
        // 卖出股票并计算盈利
        public String SellStock(string stockName, double currentPrice)
        {
            if (stockName != this.StockName)
            {
                SwitchStock(stockName);
                this.StockName = stockName;
            }
            DateTime today = DateTime.Today;
            string dateNow = today.ToString("yyyy-MM-dd");
            string buyDate = this.BuyDate.ToString("yyyy-MM-dd");
            if (dateNow == buyDate)
            {
                Console.WriteLine("当天买入的股票无法卖出，请隔天再卖。");
                return "当天买入的股票无法卖出，请隔天再卖。";
            }
            if (Quantity==0)
            {
                Console.WriteLine("持股为"+ Quantity + "。不足以出售");
                return "持股为" + Quantity + "。不足以出售";
            }
            double profit = (currentPrice - BuyPrice) * Quantity;
            Funds += currentPrice * Quantity;
            String info = $"卖出 {Quantity} 股 {StockName}，当前市值 {currentPrice}，盈利为 {profit}，资金池 {Funds}";
            Quantity = 0;
            SaveStock(stockName);
            this.SellDate = DateTime.Now;
            Console.WriteLine(info);
            return info;
        }

        // 保存虚拟数据
        public void SaveStock(string stockName)
        {
            String data = "";
            data += Quantity.ToString()+"^";
            data += BuyPrice.ToString()+"^";
            data += Funds.ToString()+"^";
            data += BuyDate.ToString("yyyy-MM-dd") + "^";
            data += BuyDate.ToString("yyyy-MM-dd");
            ControlIni.Write("Buy_" + stockName,data);
        }

        // 加载虚拟数据
        public void SwitchStock(string stockName)
        {
            String datas = ControlIni.Read("Buy_"+stockName);
            String[] data=datas.Split('^');
            if (data.Length > 4)
            {
                if (!int.TryParse(data[0], out Quantity)) Console.WriteLine("SwitchStock Quantity 解析失败") ;
                if (!double.TryParse(data[1], out BuyPrice)) Console.WriteLine("SwitchStock BuyPrice 解析失败");
                if (!double.TryParse(data[2], out Funds)) Console.WriteLine("SwitchStock Funds 解析失败");
                if(!DateTime.TryParse(data[3], out BuyDate)) Console.WriteLine("SwitchStock BuyDate 解析失败");
                if(!DateTime.TryParse(data[4], out SellDate)) Console.WriteLine("SwitchStock SellDate 解析失败");
            }
            else
            {
                Console.WriteLine("SwitchStock 数据变动导致部分无法解析，直接使用默认");
                Quantity = 0;
                Funds = 10000;
            }
            Console.WriteLine("持股编号:" + stockName);
            Console.WriteLine("持股数量:" + Quantity);
            Console.WriteLine("购买价格:" + BuyPrice);
            Console.WriteLine("资金池:" + Funds);
            Console.WriteLine("购买日期:" + BuyDate.ToString("yyyy-MM-dd"));
            Console.WriteLine("出售日期:" + SellDate.ToString("yyyy-MM-dd"));
        }
    }
}
